package ali.ali;

import java.util.Scanner;

/*
 * @Author: qianmingtian
 * @Date: 2020-05-29 16:50:37
 * @LastEditors: qianmingtian
 * @LastEditTime: 2020-07-08 09:31:53
 * @FilePath: \qianmingtian.github.io\code\ali\ali\MinimumCostSifan.java
 */
public class MinimumCostSifan {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int T = in.nextInt();

        for (int k = 0; k < T; k++) {

            long n = in.nextInt();
            long m = in.nextInt();
            long w2 = in.nextInt();
            long w3 = in.nextInt();

            long startTime = System.currentTimeMillis();
            System.out.println(startTime);

            int upper = (int) Math.ceil(Math.log(m / n) / Math.log(2));
            int lower = (int) Math.ceil(Math.log(m / n) / Math.log(3));
            System.out.println(upper + ", " + lower);

            if (n >= m) {
                System.out.println(0);
            } else {

                double result = 0;
                double sumCost = Double.POSITIVE_INFINITY;

                for (int x = lower; x <= upper; x++) {
                    for (int i = 0; i <= x; i++) {
                        result = n * Math.pow(2, i) * Math.pow(3, x - i);
                        if (result >= m) {
                            double cost = w2 * i + w3 * (x - i);
                            if (cost < sumCost) {
                                sumCost = cost;
                            }
                        }
                    }
                }
                System.out.println("minimum cost: " + (int) sumCost);
                long endTime = System.currentTimeMillis();
                System.out.println(endTime);
                System.out.println(endTime - startTime);
            }
        }
        in.close();
    }

}